"""
资源工具使用示例
"""
from k8s_deployment_extractor import K8sResourceAnalyzer

def main():
    # 创建分析器实例
    analyzer = K8sResourceAnalyzer()
    
    # 示例资源
    deployment1 = {
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "name": "example-deployment",
            "namespace": "default"
        },
        "spec": {
            "replicas": 3,
            "selector": {
                "matchLabels": {
                    "app": "example"
                }
            },
            "template": {
                "metadata": {
                    "labels": {
                        "app": "example"
                    }
                },
                "spec": {
                    "containers": [
                        {
                            "name": "example",
                            "image": "nginx:latest",
                            "ports": [
                                {
                                    "containerPort": 80
                                }
                            ]
                        }
                    ]
                }
            }
        }
    }
    
    deployment2 = {
        "apiVersion": "apps/v1",
        "kind": "Deployment",
        "metadata": {
            "name": "example-deployment",
            "namespace": "default"
        },
        "spec": {
            "replicas": 5,  # 修改副本数
            "selector": {
                "matchLabels": {
                    "app": "example"
                }
            },
            "template": {
                "metadata": {
                    "labels": {
                        "app": "example",
                        "version": "v2"  # 添加新标签
                    }
                },
                "spec": {
                    "containers": [
                        {
                            "name": "example",
                            "image": "nginx:1.19",  # 修改镜像版本
                            "ports": [
                                {
                                    "containerPort": 80
                                }
                            ],
                            "resources": {  # 添加资源限制
                                "limits": {
                                    "cpu": "500m",
                                    "memory": "512Mi"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }
    
    # 1. 验证资源
    print("\n1. 验证资源:")
    validation_result = analyzer.validate_resource(deployment1)
    print(f"验证结果: {validation_result}")
    
    # 2. 转换资源格式
    print("\n2. 转换资源格式:")
    # 转换为YAML
    yaml_str = analyzer.convert_resource(deployment1, "yaml")
    print("YAML格式:")
    print(yaml_str)
    
    # 转换为JSON
    json_str = analyzer.convert_resource(deployment1, "json")
    print("\nJSON格式:")
    print(json_str)
    
    # 3. 比较资源
    print("\n3. 比较资源:")
    diff_result = analyzer.compare_resources(deployment1, deployment2)
    print("差异摘要:")
    print(diff_result["summary"])
    print("\n详细差异:")
    print(diff_result["differences"])
    
    # 4. 合并资源
    print("\n4. 合并资源:")
    # 使用默认策略（深度合并）
    merged = analyzer.merge_resources([deployment1, deployment2])
    print("合并结果:")
    print(analyzer.convert_resource(merged, "yaml"))
    
    # 使用自定义策略
    strategy_config = {
        "spec.replicas": "replace",  # 替换副本数
        "spec.template.metadata.labels": "merge",  # 合并标签
        "spec.template.spec.containers": "replace"  # 替换容器配置
    }
    merged_with_strategy = analyzer.merge_resources_with_strategy(
        [deployment1, deployment2],
        strategy_config
    )
    print("\n使用自定义策略合并结果:")
    print(analyzer.convert_resource(merged_with_strategy, "yaml"))
    
    # 5. 验证并转换
    print("\n5. 验证并转换:")
    is_valid, converted, errors = analyzer.validate_and_convert(
        deployment1,
        "yaml"
    )
    print(f"是否有效: {is_valid}")
    if is_valid:
        print("转换结果:")
        print(converted)
    else:
        print("错误:")
        print(errors)
    
    # 6. 比较并合并
    print("\n6. 比较并合并:")
    compare_merge_result = analyzer.compare_and_merge(
        [deployment1, deployment2],
        deployment1
    )
    print("基准资源:")
    print(analyzer.convert_resource(compare_merge_result["base"], "yaml"))
    print("\n差异:")
    print(compare_merge_result["differences"])
    print("\n合并结果:")
    print(analyzer.convert_resource(compare_merge_result["merged"], "yaml"))

if __name__ == "__main__":
    main() 